<?php

/**
 * Description of model_updates
 *
 * @author Steffen Bilde
 */

class model_updates extends CI_Model
{
    function  __construct()
    {
        parent::__construct();
    }

    	//LIST UPDATES by user ($list_of_ids)
	function list_updates($list_of_ids,$limit=50)
        {
            $data = array();   
            $this->db->where_in("user_id",$list_of_ids);
            $this->db->order_by('created','desc');
            $this->db->limit($limit);           
            $Q = $this->db->get("updates");
            if ($Q->num_rows() > 0)
            {
                foreach ($Q->result() as $row)
                {
                    $data[$row->id] = $row;
                }
            }
            $Q->free_result();
            return $data;
	}

	//GET AN UPDATE by $id
	function get_update($id){
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('updates');
		if ($Q->num_rows() > 0){
			$data = $Q->row_array();
		}else{
			$data = array();
		}

		$Q->free_result();
		return $data;
	}


	//ADD UPDATE TO DB
	function add_update($status){
		$userid = $this->session->userdata('id');
		$now = date("Y-m-d h:i:s");
		$data = array(
                        'update' => $status, //Bilde
			'user_id' => $userid,
			'created' => $now
		);

		$this->db->insert("updates",$data);
	}


	//SEARCH UPDATES
	function search_updates($input){
                $term = $input; //Bilde

		$this->db->select('id,user_id,update');
		$this->db->like('update', $term);
		$Q = $this->db->get("updates");
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}else{
			$data = array();
		}
		$Q->free_result();
		return $data;
	}



}
?>
